package agenceVoyageVirtuel.server.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import agenceVoyageVirtuel.client.beans.AdresseData;
import agenceVoyageVirtuel.client.beans.ClientData;
import agenceVoyageVirtuel.client.beans.PersonneData;
import agenceVoyageVirtuel.server.DB_Conn;

public class DB_Adresse extends DB_Conn {
	/**
	 * constructor - nothing to do
	 */
	public DB_Adresse() {
		// nothing to do
	}
	
	public List<AdresseData> getAdressesUtilisees(ClientData clientCourant) {
		String query = "SELECT t_adresse.idAdresse, numero, rue, ville, codePostal, t_personne.idPersonne, prenom, nom"
				+ " FROM t_commande JOIN t_adresse, t_personne"
				+ " WHERE idClient=" + clientCourant.getIdClient()
				+ " AND t_commande.idAdresse=t_adresse.idAdresse"
				+ " AND t_adresse.idPersonne=t_personne.idPersonne;";

		// prepare for rpc transport
		List<AdresseData> adresseDatas = new ArrayList<AdresseData>();
		
		try {
			Connection connection = getConn();
			Statement select = connection.createStatement();
			ResultSet result = select.executeQuery(query);
			int i = 0;
			
			while (result.next()) {
				adresseDatas.add(new AdresseData());
				adresseDatas.get(i).setIdAdresse(result.getInt(1));
				adresseDatas.get(i).setNumero(result.getInt(2));
				adresseDatas.get(i).setRue(result.getString(3));
				adresseDatas.get(i).setVille(result.getString(4));
				adresseDatas.get(i).setCodePostal(result.getInt(5));
				//Personne
				adresseDatas.get(i).setPersonne(new PersonneData());
				adresseDatas.get(i).getPersonne().setIdPersonne(result.getInt(6));
				adresseDatas.get(i).getPersonne().setPrenom(result.getString(7));
				adresseDatas.get(i).getPersonne().setNom(result.getString(8));
				
				i++;
			}

			// clean up
			result.close();
			connection.close();
		} catch(Exception e) {
			System.err.println("Mysql Statement Error: " + query);
			e.printStackTrace();
		}

		// return datas
		return adresseDatas;		
	}
	
	private int chercherAdresse(AdresseData adresse) {
		String query = "SELECT idAdresse"
				+ " FROM t_adresse"
				+ " WHERE numero=" + adresse.getNumero()
				+ " AND rue=" + adresse.getRue()
				+ " AND ville=" + adresse.getVille()
				+ " AND codePostal=" + adresse.getCodePostal()
				+ ";";
	
		// prepare for rpc transport
		int id = -1;
		
		try {
			Connection connection = getConn();
			Statement select = connection.createStatement();
			ResultSet result = select.executeQuery(query);
			
			while(result.next()) {
				id = result.getInt(1); //idAdresse
				break;
			}
	
			// clean up
			result.close();
			connection.close();
		} catch(Exception e) {
			System.err.println("Mysql Statement Error: " + query);
			e.printStackTrace();
		}
	
		// return datas
		return id;		
	}
	
	public void creerAdresse(AdresseData adresse) {
		new DB_Personne().creerPersonne(adresse);
		adresse.setIdAdresse(chercherAdresse(adresse));
		
		if(adresse.getIdAdresse() != -1)
			return;
			
		String query = "INSERT INTO t_adresse(numero, rue, ville, codePostal)"
				+ " VALUES (" + adresse.getNumero()
				+ ", " + adresse.getRue()
				+ ", " + adresse.getVille()
				+ ", " + adresse.getCodePostal()
				+ ");";
		
		try {
			Connection connection = getConn();
			PreparedStatement  prepareStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
			prepareStatement.executeUpdate();
	        ResultSet keys = prepareStatement.getGeneratedKeys();
	        
	        while(keys.next()) {
	        	adresse.setIdAdresse(keys.getInt(1)); //idAdresse
	        	break;
	        }
			
	        keys.close();
	        prepareStatement.close();
			connection.close();
		} catch(Exception e) {
			System.err.println("Mysql PrepareStatement Error: " + query);
			e.printStackTrace();
		}
		
		return;					
	}
}
